package com.rzjm.exercise.module.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rzjm.exercise.common.base.controller.BaseController;
import com.rzjm.exercise.common.base.data.DataTableLayUI;
import com.rzjm.exercise.common.log.annotation.Log;
import com.rzjm.exercise.module.domain.FireWork;
import com.rzjm.exercise.module.domain.Shop;
import com.rzjm.exercise.module.domain.SmokeAlarmInfo;
import com.rzjm.exercise.module.service.ShopService;
import com.rzjm.exercise.module.service.SmokeAlarmInfoService;
import com.rzjm.exercise.sys.system.user.domain.UserDO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Controller
@RequestMapping(value = "/module/smoke/record",produces ="application/json;charset=UTF-8")
public class SmokeAlarmInfoRecordController extends BaseController {
    @Resource
    private SmokeAlarmInfoService smokeAlarmInfoService;
    @Resource
    private ShopService shopService;

    @Value("${projecturl.imgurl}")
    private String imgUrl;

    @Log("查询烟感记录页面")
    @GetMapping("")
    @RequiresPermissions("module:smoke:record:smoke")
    String eventsRecord(Model model) {
        UserDO user = getUser();
        QueryWrapper<Shop> wrapper = new QueryWrapper<>();
        wrapper.eq("state", 1);
        wrapper.ne("shop_type", 3);
        if (user.getTownId() != null && user.getTownId() > 0) {
            wrapper.eq("town_id", user.getTownId());
        }
        if (user.getGroupId() != null && user.getGroupId() > 0) {
            wrapper.eq("group_id", user.getGroupId());
        }
        List<Shop> shopList = shopService.list(wrapper);

        model.addAttribute("shopList", shopList);
        return "module/device/yangan/record/list";
    }


    @Log("查询举报记录页面")
    @ResponseBody
    @GetMapping("/list")
    public DataTableLayUI<SmokeAlarmInfo> list(DataTableLayUI<SmokeAlarmInfo> condition) {
        UserDO userDO = getUser();
        Map<String, Object> map = condition.getSearchParams();
        Integer companyId = map.containsKey("companyId") && StrUtil.isNotEmpty(map.get("companyId").toString()) ?
                Integer.parseInt(map.get("companyId").toString()) : null;
        QueryWrapper<SmokeAlarmInfo> qw = new QueryWrapper<>();
        if(userDO.getUserType() == 2){//集团管理员
//            qw.eq("group_id",userDO.getGroupId());
            List<Shop> shopList = shopService.selectListByColumn("group_id",userDO.getGroupId());
            if (shopList != null && shopList.size() > 0){
                List<Integer> shopIds = shopList.stream().map(s -> s.getId()).collect(Collectors.toList());
                qw.in("company_id", shopIds);
            }else {
                return  new DataTableLayUI<SmokeAlarmInfo>();
            }
            if(companyId!= null){
                qw.eq("company_id", companyId);
            }
        }else if(userDO.getUserType() == 3){//片区管理员
//            qw.eq("pianqu_id",userDO.getPianquId());
            List<Shop> shopList = shopService.selectListByColumn("pianqu_id",userDO.getGroupId());
            if (shopList != null && shopList.size() > 0){
                List<Integer> shopIds = shopList.stream().map(s -> s.getId()).collect(Collectors.toList());
                qw.in("company_id", shopIds);
            }else {
                return  new DataTableLayUI<SmokeAlarmInfo>();
            }
            if(companyId!= null){
                qw.eq("company_id", companyId);
            }
        }else if(userDO.getUserType() == 4){//分厂管理员
            qw.eq("company_id",userDO.getShopId());
        } else if(userDO.getUserType() == 10){//企业管理员
            qw.eq("company_id",userDO.getShopId());
        } else {
            if(companyId!= null){
                qw.eq("company_id",companyId);
            }
        }
        condition.setQw(qw);
        return smokeAlarmInfoService.pageSearchLayUI(condition);
    }

}
